package com.test.controller;

import com.test.pojo.Books;
import com.test.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //controller层调service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部的数据并且返回到书籍展示页面
    @RequestMapping("/allBook")
    public String list(@RequestParam(required = false,defaultValue = "",value="searchName") String name, Model model){
        System.out.println(name);
        List<Books> list = bookService.queryAllBook(name);
        model.addAttribute("list",list);
        model.addAttribute("searchName",name);
        return "allBook";
    }
    //增加书籍
    @RequestMapping("/toAddPaper")
    public String toAddPaper(){
        return "addBook";
    }
    //执行添加书籍
    @PostMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    @GetMapping("/toUpdatePaper")
    public String toUpdatePaper(@RequestParam("id") int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("books",books);
        return "updateBook";
    }
    @PostMapping("/updateBook")
    public String updateBook(@RequestParam("id") int id,Books books){
        books.setBookID(id);
        System.out.println(books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }
    @GetMapping("/deleteBook")
    public String deleteBook(@RequestParam("id") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
}
